
#include "iUiButton.h"

iUiButton::iUiButton(void)
{
	m_imgPressed = NULL;
	m_imgNormal = NULL;
	m_bIsClipExtend = FALSE;
	SetRect(&m_rcExtendSize, 0, 0, 0, 0);

	SetButtonType(MZC_BUTTON_NONE);
	EnablePressedHoldSupport(true);
	SetPressedHoldTime(2500);
	m_bNeedDrawNormal = TRUE;
}

iUiButton::~iUiButton(void)
{
}

void iUiButton::PaintWin(HDC hdc, RECT* prcWin, RECT* prcUpdate)
{
	//MzDrawControl(hdc, prcWin, GetButtonType(), GetState(), false);

	RECT rc = *prcWin;

	if(GetState() == MZCS_BUTTON_PRESSED)
	{
		if((m_imgPressed != NULL))
		{
			RECT rcExtend = rc;
			if(m_bIsClipExtend)
			{
				rcExtend.left -= m_rcExtendSize.left;
				rcExtend.top -= m_rcExtendSize.top;
				rcExtend.right += m_rcExtendSize.right;
				rcExtend.bottom += m_rcExtendSize.bottom;
			}
			m_imgPressed->Draw(hdc, &rcExtend, false, false);
		}
//jdhuang 20091223
/*
		if(m_bNeedDrawNormal)
		{
			if((m_imgNormal != NULL))	
			{
			m_imgNormal->Draw(hdc, &rc, false, false);		
			}
		}
*/
	}
	else
	{
		if((m_imgNormal != NULL))
		{
			m_imgNormal->Draw(hdc, &rc, false, false);	
		}
	}

	
	if (GetText().Length())
	{
		HFONT font = FontHelper::GetFont(GetTextSize());
		HGDIOBJ oldfont = SelectObject(hdc, font);
		::SetTextColor(hdc, GetTextColor());
		SetBkMode(hdc, TRANSPARENT);
		RECT rcText = *prcWin;
		DrawText(hdc, GetText(), GetText().Length(), &rcText, DT_VCENTER|DT_CENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
		SelectObject(hdc, oldfont);
	}
}

void iUiButton::UiSetClipExtend(BOOL bEnable, RECT &rcSize)
{
	m_bIsClipExtend = bEnable;
	m_rcExtendSize = rcSize;

	if(m_bIsClipExtend)
	{
		EnableClipExtend(true);
		
		int nMax = max(rcSize.top, rcSize.right);
		nMax = max(nMax, rcSize.left);
		nMax = max(nMax, rcSize.bottom);

		SetClipExtend(nMax);
	}
}
